package logic

import (
	"context"
	"database/sql"

	"mail/service/property/rpc/internal/svc"
	"mail/service/property/rpc/property"

	"github.com/dtm-labs/dtmgrpc"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"google.golang.org/grpc/status"
)

type CurtailStockRevertLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCurtailStockRevertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CurtailStockRevertLogic {
	return &CurtailStockRevertLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 恢复
func (l *CurtailStockRevertLogic) CurtailStockRevert(in *property.StockRequest) (*property.StockResponse, error) {
	// todo: add your logic here and delete this line
	//获取数据库连接
	db, err := sqlx.NewMysql(l.svcCtx.Config.Mysql.DataSource).RawDB()
	if err != nil {
		return nil, status.Error(500, err.Error())
	}
	//获得子事务屏障对象
	barrier, err := dtmgrpc.BarrierFromGrpc(l.ctx)
	if err != nil {
		return nil, status.Error(500, err.Error())
	}
	err = barrier.CallWithDB(db, func(tx *sql.Tx) error {
		//更新资产库存
		_, err := l.svcCtx.PropertyModel.TxAdjustStock(tx, in.Id, 1)
		return err
	})
	if err != nil {
		return nil, err
	}
	return &property.StockResponse{}, nil
}
